package com.mea.server;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.bluetooth.*;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;

public class Server {

    static final String serverUUID = "11111111111111111111111111111123";
    
    public static native int pause(int value);

    static 
    {
        //System.loadLibrary("MathLib");
        System.load("C:/simple/Media.dll");
		//System.load("MathLib.dll");
       }
    
    public static InputStream in;
    public static OutputStream out;
    private static boolean ServerRun = true;

    public static void main(String[] args) {
    	
    	new Server();
    }
    
    public Server() {
    	
    	log("Starting Server");
    	
        try {
			LocalDevice.getLocalDevice().setDiscoverable(DiscoveryAgent.GIAC);
		} catch (BluetoothStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		String url = "btspp://localhost:" + serverUUID + ";name=serialCOMM";
		log("Create server by url: " + url);
        
        try {
        	StreamConnectionNotifier notifier = (StreamConnectionNotifier) Connector.open(url);
        	
        	serverLoop(notifier);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    private void serverLoop(final StreamConnectionNotifier notifier) {
        Thread handler = new Thread() {

            @Override
            public void run() {

            	while (ServerRun) { // infinite loop to accept connections.
            		
            		log("Waiting for connection...");

            		try {
						handleConnection(notifier.acceptAndOpen());
					} catch (IOException e) {
						log(e);
					}
              
                    try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						log(e);
					}
            	}
               
            }
        };
        handler.start();
    }
    
    private void handleConnection(StreamConnection conn) throws IOException {
        
    	out = conn.openOutputStream();
        in = conn.openInputStream();
       
        startClientThread(in, out);
        
        log("Connection found...");
    }
    
    private void startClientThread(final InputStream in, final OutputStream out) {

        Thread reader = new Thread() {

            @Override
            public void run() {

                byte[] s = new byte[512];
                log("Waiting for data");
             
                while (true) {

                	try {
						int r = in.read(s);
						if(r != -1) {
							System.out.println("\n");
							String myString = new String(s, 0, r);
							System.out.println(myString);
							System.out.println("\n");
							//System.out.println(Server.pause(1));
							Server.pause(1);
						}
						else // client disconnect
							break;
												
						
					} catch (IOException e) {
						log(e);
					}

                }
            }
        };
        reader.start();
    }
    
    

    
    private void log(String msg) {
    	System.out.println(msg);
    }
    
    private void log(Throwable e) {
        log(e.getMessage());

        e.printStackTrace();
    }
}
